﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;
using SharedLibraries;
using ThangNM_LIB.FUNC;
using System.Web.Script.Serialization;

namespace CSharp.Admin
{
    public partial class AccessUI : System.Web.UI.Page
    {
        public FuncEO objFunc
        {
            get { return (FuncEO)ViewState["objFunc"]; }
            set { ViewState["objFunc"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            MsgUC1.setClearMsg();
            if (!IsPostBack)
            {
                try
                {
                    if (Request.QueryString["r"] != null)
                    {
                        String key = Security.Login.Decrypt(Request.QueryString["r"], System.Configuration.ConfigurationManager.AppSettings["PRIVATE_KEY"]);
                        Int32 _PK_iFunctionID = 0;
                        if (Int32.TryParse(key, out _PK_iFunctionID))
                        {
                            objFunc = (new ShareDAO.FuncDAO()).SelectItem(_PK_iFunctionID);
                            if (!CommonsCheck.Object_OK(objFunc))
                            {
                                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Chức năng không tồn tại");
                                return;
                            }
                            Page.Title = "Quản trị - " + objFunc.sName;
                            if (objFunc.bStatus == false)
                            {
                                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Chức năng đã bị khóa");
                                return;
                            }
                            else
                            {
                                key = Security.getTokenFunc(objFunc, true, objFunc.sToken);
                                if (String.IsNullOrEmpty(key))
                                {
                                    phMain.Controls.Add(Page.LoadControl(OtherEx.getAppSettings("RawUrl_UC", "~/usercontrol/") + objFunc.sPath));
                                    return;
                                }
                                else
                                {
                                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, key);
                                    return;
                                }

                            }
                        }
                        else
                        {
                            MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, String.Format("Dữ liệu nhập vào [{0}] không đúng định dạng số", key));
                            return;
                        }
                    }
                    else
                        MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, String.Format("Sai đường dẫn chức năng"));
                }
                catch (Exception ex)
                {
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
                }
            }

            if (objFunc == null)
            {
                objFunc = new FuncEO();
                objFunc.sPath = "AdminUC.ascx";
            }
            phMain.Controls.Add(Page.LoadControl(OtherEx.getAppSettings("RawUrl_UC", "~/usercontrol/") + objFunc.sPath));



            //List<KeyValueEO> lst = new List<KeyValueEO>();
            //lst.Add(new KeyValueEO("a", "x"));
            //lst.Add(new KeyValueEO("b", "y"));
            //lst.Add(new KeyValueEO("c", "z"));
            //divThangNM_DataTextBox1 = ConvertEx.Convert_KeyValueEO_StringOfArray(lst, true, true);
            //divThangNM_DataTextBox2 = ConvertEx.Convert_KeyValueEO_StringOfArray(lst, false, true);

            //MsgUC1.setClearMsg();
            //if (Request.QueryString["t"] != null)
            //{
            //    if (Convert.ToString(Request.QueryString["t"]).Equals("admin"))
            //        phMain.Controls.Add(Page.LoadControl("~/UserControl/AdminUC.ascx"));
            //    if (Convert.ToString(Request.QueryString["t"]).Equals("hotdeal"))
            //        phMain.Controls.Add(Page.LoadControl("~/UserControl/HotDeal_NewUC.ascx"));
            //    else if (Convert.ToString(Request.QueryString["t"]).Equals("hotdealex"))
            //        phMain.Controls.Add(Page.LoadControl("~/UserControl/HotDeal_NewExUC.ascx"));
            //    else if (Convert.ToString(Request.QueryString["t"]).Equals("seo"))
            //        phMain.Controls.Add(Page.LoadControl("~/UserControl/SEO_NewUC.ascx"));
            //    else if (Convert.ToString(Request.QueryString["t"]).Equals("seoex"))
            //        phMain.Controls.Add(Page.LoadControl("~/UserControl/SEO_NewExUC.ascx"));
            //    else if (Convert.ToString(Request.QueryString["t"]).Equals("setting"))
            //        phMain.Controls.Add(Page.LoadControl("~/UserControl/Setting_NewUC.ascx"));
            //    else if (Convert.ToString(Request.QueryString["t"]).Equals("settingex"))
            //        phMain.Controls.Add(Page.LoadControl("~/UserControl/Setting_NewExUC.ascx"));
            //}
            //else
            //    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Đường dẫn chức năng sai.");
        }

        public void setClearMsg()
        {
            MsgUC1.setClearMsg();
        }

        public void setNotifications(Int16 iType, string strMsg, String strMsgFull = null)
        {
            MsgUC1.setNotifications(iType, "[ASPX]" + strMsg, strMsgFull);
        }

        protected String divABC_DataTextBox0;
        protected String divContainerTextBox2;
        protected void btnPost_Click(object sender, EventArgs e)
        {
            String[] txtTemp;
            JavaScriptSerializer jss = new JavaScriptSerializer();

            txtTemp = Request.Form.GetValues("divABC_ValueTextBox0");
            divABC_DataTextBox0 = ConvertEx.ToBase64(jss.Serialize(txtTemp), System.Text.Encoding.UTF8);

            //string[] textboxValues1 = Request.Form.GetValues("divContainerTextBox1_Item");
            //JavaScriptSerializer serializer1 = new JavaScriptSerializer();
            //divContainerTextBox1 = serializer1.Serialize(textboxValues1);


            //string[] textboxValues2 = Request.Form.GetValues("divContainerTextBox2_Item");
            //JavaScriptSerializer serializer2 = new JavaScriptSerializer();
            //divContainerTextBox2 = serializer2.Serialize(textboxValues2);
        }


        protected String divThangNM_DataTextBox1;
        protected String divThangNM_DataTextBox2;
        protected String divDuongNTH_DataTextBox1;
        protected String divDuongNTH_DataTextBox2;
        protected void btnGet_Click(object sender, EventArgs e)
        {
            String[] txtTemp;
            JavaScriptSerializer jss = new JavaScriptSerializer();

            List<KeyValueEO> lst = ConvertEx.Convert_ArrayString_KeyValueEO(Request.Form.GetValues("divThangNM_ValueTextBox1"), Request.Form.GetValues("divThangNM_ValueTextBox2"));

            divThangNM_DataTextBox1 = ConvertEx.Convert_KeyValueEO_StringOfArray(lst, true, true);
            divThangNM_DataTextBox2 = ConvertEx.Convert_KeyValueEO_StringOfArray(lst, false, true);

            //txtTemp = Request.Form.GetValues("divThangNM_ValueTextBox1");
            //divThangNM_DataTextBox1 = ConvertEx.ToBase64(jss.Serialize(txtTemp), System.Text.Encoding.UTF8);

            //txtTemp = Request.Form.GetValues("divThangNM_ValueTextBox2");
            //divThangNM_DataTextBox2 = ConvertEx.ToBase64(jss.Serialize(txtTemp), System.Text.Encoding.UTF8);


            txtTemp = Request.Form.GetValues("divDuongNTH_ValueTextBox1");
            divDuongNTH_DataTextBox1 = ConvertEx.ToBase64(jss.Serialize(txtTemp), System.Text.Encoding.UTF8);

            txtTemp = Request.Form.GetValues("divDuongNTH_ValueTextBox2");
            divDuongNTH_DataTextBox2 = ConvertEx.ToBase64(jss.Serialize(txtTemp), System.Text.Encoding.UTF8);
        }

        protected void btnGetID_Click(object sender, EventArgs e)
        {
            //Timer1.Enabled = false;
            txtInput.Text = Security.getKey(62);
            //if (Timer1.Enabled)
            //    Timer1.Enabled = false;
            //else
            //    Timer1.Enabled = true;
        }

        //protected void Timer1_Tick(object sender, EventArgs e)
        //{
        //    if (String.IsNullOrEmpty(txtInput.Text))
        //        txtInput.Text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~abcdefghijklmnopqrstuvwxyz0123456789";
        //    //"ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~abcdefghijklmnopqrstuvwxyz0123456789"
        //    txtInput.Text = Security.getKey(92);
        //}
    }
}
//lblMsg.Text = OtherEx.LoadMenu(1, 0, 0);
//txtInput.Text = (new Security()).Check_Access(0);